var express = require('express');
const UserModel = require('../../module/userModel');
const md5 = require('md5');
const jwt = require('jsonwebtoken')
const {secret} = require('../../config/dc')

var router = express.Router();


router.post('/login', (req, res) => {
    let { username, password } = req.body
    UserModel.findOne({ username, password: md5(password) }, (err, data) => {
        if (err) {
            return res.json({
                code: '2001',
                msg: '用户检索失败',
                data: null
            })
        }
        if (!data) {
            return res.json({
                code: '2002',
                msg: '用户名或密码不正确',
                data: null
            })
        }
        let token = jwt.sign({ username: data.username, _id: data._id }, secret, { expiresIn: 60 * 60 * 24 * 7 })

        res.json({
            code: '0000',
            msg: '登录成功',
            data: token
        })
    })
})

module.exports = router;
